﻿--[[
   @brief    view module for poptips ui
   @author   dragonqiu
   @version  2.0
   @date     2015-012-12
--]]

poptips_view =
{
    -- 窗口默认尺寸
    width = 480;
    height = 230;
    shadow_px = 0;
    last_opt_ = nil;
};

poptips_icons = {
    kError =    "ONEIMAGE:bottom_bar\\tips_icon\\red_icon.png;Stretch;0;";
    kWarning =  "ONEIMAGE:bottom_bar\\tips_icon\\yellow_icon.png;Stretch;0;";
    kSuccess =  "ONEIMAGE:bottom_bar\\tips_icon\\green_icon.png;Stretch;0;";
    kInfo =     "ONEIMAGE:bottom_bar\\tips_icon\\blue_icon.png;Stretch;0;";
};

poptips_highlight_btn = {
    Background = "ONEIMAGE:client_tips\\btn2_00.png;Stretch;null;";
    BackgroundHover = "ONEIMAGE:client_tips\\btn2_01.png;Stretch;null;";
    BackgroundPressed = "ONEIMAGE:client_tips\\btn2_02.png;Stretch;null;";
};

poptips_colors = {
    kHighlight = "RGB:255,200,0",
    kHighlightOversea = "RGB:25,178,255",
    kTitleNormal = "RGB:60,60,60",
    kContentNormal = "RGB:140,140,140",
};

function poptips_view:init()
    ierd_tgp.tpf_ui.Require("poptips\\server_tips_view.lua");
    ierd_tgp.tpf_ui.Log("[poptips_view] init.");
end;

function poptips_view:toStr(str)
    local utfs, utfh = U8toCUIStr(str);
    return utfs;
end;

-- 通过ID获得当前窗口配置
-- TODO 不仅要保存数据，还要记得清理数据
function poptips_view:get_ctr_data(win_id)
    return poptips_model.ctr_data[win_id] or 0;
end;


function poptips_view:exist(table_hash)
    for win_id, opt in pairs(poptips_model.ctr_data) do
        if table_hash == opt.table_hash then
            return true;
        end
    end

    return false;
end;


function poptips_view:delay_close_window(user_data)
    base_utility.remove_interval("poptips_view:delay_close_window", user_data);
    if nil == user_data then
        return;
    end
    self:_close_impl(ui_utility.uilib(), user_data);
end;

-- 关闭处理
function poptips_view:_close_impl(uilib, win_id)
    -- 手动关闭
    if win_id > 0 then
        local opts = self:get_ctr_data(win_id);
        local pControl = uilib:GetControlPtr(win_id);
        local pPoptipsDlg = tolua.cast(pControl, "Tenio::ITDialog");

        if not pPoptipsDlg then return false end;

        if type(opts) == "table" and
            tostring(opts.modal) == "true" then
            ui_utility.end_fake_modal_by_id(win_id, nil, opts.parent_id);
        else
            pPoptipsDlg:Show(FALSE);
        end;
        ui_utility.remove_wnd_opts(win_id);
        layout_mgr.delay_remove_ctrl(win_id);
        poptips_model.ctr_data[win_id] = nil;
        return true;
    end

    return false;
end;

 function poptips_view:_close(uilib, win_id)
    local opts = self:get_ctr_data(win_id);

    if opts == nil or opts == 0 then
        return;
    end;

    if self:_close_impl(uilib, win_id) then
        -- 处理旧版配置的取消按钮
        if opts.cancelBtn and opts.close_means_cancel and
           opts.cancelBtn.handle and opts.cancelBtn.handle ~= "" then
            ierd_tgp.tpf_ui.Log("[poptips_view] trigger cancelBtn.handle.");
            local addon_info = self:get_addon_info(win_id);
            opts.cancelBtn.handle(uilib, win_id, opts.user_data, addon_info);
        end;
        if (server_tips_view ~= nil) then
            server_tips_view.cancelQblinkRestartListener();
        end
        ierd_tgp.tpf_ui.Log("[poptips_view] close poptips window success.");
        return true;
    end

    return false;
end;

-- 新版关闭按钮处理(手动关闭)
function poptips_view:new_win_close(uilib, win_id)
    base_utility.log("[poptips_view]new_close, win_id:" .. tostring(win_id));
    if win_id ~= nil and win_id > 0 then
        local opts = self:get_ctr_data(win_id);  
        local addon_info = self:get_addon_info(win_id);

        if self:_close(uilib, win_id) then
            if opts.manuallyCloseCallback then
                opts.manuallyCloseCallback(win_id, opts.user_data, addon_info);
            end
            if opts.cancel_service ~= nil then
                local ret_data = {};
                ret_data.cancel_service_data = opts.cancel_service_data or {};
                ret_data.dont_ask_checked = addon_info.cbx_dont_ask_checked;
                comm_center.send_broadcast(opts.cancel_service, ret_data);
            end
            if (server_tips_view ~= nil) then
                server_tips_view.cancelQblinkRestartListener();
            end
            if opts.dontaskCallback then
                opts.dontaskCallback(win_id, opts.user_data, addon_info);
            end
        end
    end
end;
--
function poptips_view:new_close(uilib, ctr_id)
    base_utility.log("[poptips_view]new_close, ctrl_id:" .. tostring(ctr_id));
    local title_id = ui_utility.get_ctrl_parent_id(ctr_id);
    local win_id = ui_utility.get_ctrl_parent_id(title_id);
    poptips_view:new_win_close(uilib, win_id);
end;

-- 旧版关闭按钮处理
function poptips_view.close(uilib)
    base_utility.log("[poptips_view]close");
    poptips_view:new_win_close(uilib, poptips_view.win_id);
end

-- 按钮处理
function poptips_view:extraBtnClick(uilib, ctr_id)
    local btn_group_id = ui_utility.get_ctrl_parent_id(ctr_id);
    local win_id = ui_utility.get_ctrl_parent_id(btn_group_id);
    local addon_info = self:get_addon_info(win_id);
    self:on_extra_clicked(win_id, addon_info);
end;

function poptips_view:on_extra_clicked(win_id, addon_info)
    base_utility.log("[poptips_view]on_extra_clicked, win_id:" .. tostring(win_id));

    local opts = self:get_ctr_data(win_id);
    local uilib = ui_utility.uilib();
    if opts.extraBtn.handle ~= nil and opts.extraBtn.handle ~= "" then
        opts.extraBtn.handle(uilib, ctr_id, opts.user_data, addon_info);
    end
    if opts.extraBtn.close_win == true then
        self:_close_impl(uilib, win_id);
        if opts.dontaskCallback then
            opts.dontaskCallback(win_id, opts.user_data, addon_info);
        end
    end
end;

-- 确定按钮处理
function poptips_view:okBtnClick(uilib, ctr_id)
    local btn_group_id = ui_utility.get_ctrl_parent_id(ctr_id);
    local win_id = ui_utility.get_ctrl_parent_id(btn_group_id);
    local addon_info = self:get_addon_info(win_id);
    poptips_view:on_ok_clicked(win_id, addon_info);
end;

    -- OK点击
function poptips_view:on_ok_clicked(win_id, addon_info)
    base_utility.log("[poptips_view]on_ok_clicked, win_id:" .. tostring(win_id));

    local opts = self:get_ctr_data(win_id);

    if opts == nil or opts == 0 then
        return;
    end;

    local uilib = ui_utility.uilib();
    if opts.okBtn.handle ~= nil and opts.okBtn.handle ~= "" then
        opts.okBtn.handle(uilib, win_id, opts.user_data, addon_info);
    end
    if opts.ok_service ~= nil then
        local ret_data = {};
        ret_data.ok_service_data = opts.ok_service_data or {};
        ret_data.dont_ask_checked = addon_info.cbx_dont_ask_checked;
        comm_center.send_broadcast(opts.ok_service, ret_data);
    end
    if opts.okBtn.close_win == true then
        if opts.okBtn.delay_close == true then
            ui_utility.show_window(win_id, false);
            base_utility.add_interval("poptips_view:delay_close_window", 5000,
                                       win_id);
        else
            self:_close_impl(uilib, win_id);
        end
        if opts.dontaskCallback then
            opts.dontaskCallback(win_id, opts.user_data, addon_info);
        end
    end
end;
   

-- 取消按钮处理
function poptips_view:cancelBtnClick(uilib, ctr_id)
    local title_id = ui_utility.get_ctrl_parent_id(ctr_id);
    local win_id = ui_utility.get_ctrl_parent_id(title_id);
    local addon_info = self:get_addon_info(win_id);
    poptips_view:on_cancel_clicked(win_id, addon_info);
end;

-- web框Cancel点击
function poptips_view:on_cancel_clicked(win_id, addon_info)
    base_utility.log("[poptips_view]jc_cancel_clicked, win_id:" .. tostring(win_id));

    local opts = self:get_ctr_data(win_id);
    if opts == 0 then
        return;
    end;
    local uilib = ui_utility.uilib();
    if self:_close_impl(uilib, win_id) then
        if opts.manuallyCloseCallback then
            base_utility.log("[poptips_view][jc_cancel_clicked]manuallyCloseCallback, win_id:"
                    .. tostring(win_id));
            opts.manuallyCloseCallback(win_id, opts.user_data, addon_info);
        end  
        if opts.cancel_service ~= nil then
            base_utility.log("[poptips_view][jc_cancel_clicked]send cancel_service, win_id:"
                    .. tostring(win_id));
            local ret_data = {};
            ret_data.cancel_service_data = opts.cancel_service_data or {};
            ret_data.dont_ask_checked = addon_info.cbx_dont_ask_checked;
            comm_center.send_broadcast(opts.cancel_service, ret_data);
        end
        -- 处理旧版配置的取消按钮
        if opts.cancelBtn and opts.cancelBtn.handle and opts.cancelBtn.handle ~= "" then
            ierd_tgp.tpf_ui.Log("[poptips_view][cancelBtnClick] trigger cancelBtn.handle.");
            opts.cancelBtn.handle(uilib, win_id, opts.user_data, addon_info);
        end;
        if opts.dontaskCallback then
            opts.dontaskCallback(win_id, opts.user_data, addon_info);
        end
    end
end;

-- 链接跳转处理
function poptips_view:okLinkClick(uilib, ctr_id)
    local con_id = ui_utility.get_ctrl_parent_id(ctr_id);
    local win_id = ui_utility.get_ctrl_parent_id(con_id);
    local opts = self:get_ctr_data(win_id);  
    local addon_info = self:get_addon_info(win_id);
    opts.hyperLinkHandle(uilib, win_id, opts.user_data, addon_info);
end;


function poptips_view:onGotoLinkClick(uilib, ctr_id)
    local url = self._settings.link_goto.link;
    web_utility.open_link(url);
    self:cancelBtnClick(uilib, ctr_id);
end;

function poptips_view:get_addon_info(win_id)    
    local addon_info = {
        cbx_dont_ask_checked = false;
    };
    -- 不再询问勾选框
    local cbx_dont_ask_id = ui_utility.get_ctrl_id_by_path(win_id,   "Container_1\\cbx_dont_ask");   
    local component = ui_utility.get_ctrl_ptr_by_id(cbx_dont_ask_id);
    if component ~= nil and component:IsChecked() == TRUE then
        addon_info.cbx_dont_ask_checked = true;
    end

    return addon_info;
end;

-- Poptips默认布局脚本
-- TODO 默认布局需要单独拎出一个文件来
-- TODO 链接文字和普通文字不需要区分使用控件，都应使用richTextBox

-- WeGame新弹窗样式，显示区域大小为480*230，左右阴影各为16，上6，下26，
-- 为兼容原有400*200的弹窗，使用hardcode自适应调整
-- 后续通用tips大小统一设置为480*230
-- by maxyao 05/27/2017

function poptips_view:default_layout(win_id)
    local uilib = ui_utility.uilib();
    local opts = self._settings;

    -- 标题栏
    local title_bar_id = ui_utility.get_ctrl_id_by_path(win_id, "TitleBar1");
    ui_utility.setproperty_by_id(title_bar_id, 'Width', opts.width);

    -- 标题
    local titleUf8Str, holderStr = U8toCUIStr(opts.title);
    local nTitle = ui_utility.get_ctrl_id_by_path(win_id, "TitleBar1\\TitleText");
    local pTitleControl = uilib:GetControlPtr(nTitle);
    local pTitle = tolua.cast(pTitleControl, "Tenio::ITStatic");
    pTitle:SetText(titleUf8Str);

    -- 内容区域
    local con_id = ui_utility.get_ctrl_id_by_path(win_id, "Container_1");
    ui_utility.setproperty_by_id(con_id, 'Width', opts.width-self.shadow_px);
    ui_utility.setproperty_by_id(con_id, 'Height', opts.height-self.shadow_px-40);
    -- 主区域

    -- 提示图标
    local btnGroupW = 210;
    local iconGroupW = 50;
    local max_width = opts.width-self.shadow_px-(38*2)-50-20;
    local iconExist = (opts.icon ~= nil and opts.icon ~= "");
    local tipsIconExist = (opts.tipsIcon ~= nil and opts.tipsIcon ~= "");

    local icon_group_id = ui_utility.get_ctrl_id_by_path(win_id, "Container_1\\icon_group");
    local icon_id = ui_utility.get_ctrl_id_by_path(win_id,"Container_1\\icon_group\\Iconbox_0");

    local icon_group_yoffset = (opts.height-self.shadow_px-40-48-50)/2;
    if iconExist or tipsIconExist then
        if iconExist then
            SetBackground_ID(uilib, icon_id, opts.icon);
        else
            SetBackground_ID(uilib, icon_id, opts.tipsIcon);
        end;
        ui_utility.setproperty_by_id(icon_id, "Visible", "true");
        ui_utility.setproperty_by_id(icon_group_id, "YOffset", icon_group_yoffset);
    else
        ui_utility.setproperty_by_id(icon_id, "Visible", "false");
    end;

    -- 文字内容
    local text_content = opts.desc;
    local interface = "";
    local is_hyperLink = false;
    local link_id = ui_utility.get_ctrl_id_by_path(win_id, "Container_1\\link_content");
    local text_id = ui_utility.get_ctrl_id_by_path(win_id, "Container_1\\content");
    local show_conten_id = nil;
    local content_xoffset = 108;
    if iconExist == false and tipsIconExist == false then
        content_xoffset = content_xoffset-50-20;
        max_width = max_width + 50 + 20;
    end;

    if opts.hyperLink ~= nil and tostring(opts.hyperLink) == "true" then
        ui_utility.setproperty_by_id(link_id, 'Visible', "true");
        ui_utility.setproperty_by_id(text_id, 'Visible', "false");
        interface = "Tenio::IRichTextBox";
        is_hyperLink = true;
        show_conten_id = link_id;
        ui_utility.setproperty_by_id(show_conten_id, "HyperLinkColor", self.ui_highlight_color);
    else
        ui_utility.setproperty_by_id(text_id, 'Visible', "true");
        ui_utility.setproperty_by_id(link_id, 'Visible', "false");
        interface = "Tenio::ITStatic";
        show_conten_id = text_id;
    end;
    
    local pConTextControl = uilib:GetControlPtr(text_id);
    local pConText = tolua.cast(pConTextControl, interface);

    local text_title = opts.descTitle;
    local rich_text_title = opts.descRichTitle;

    local title_id = 0;
    local static_title_id = ui_utility.get_ctrl_id_by_path(win_id, "Container_1\\title");
    local rich_title_id = ui_utility.get_ctrl_id_by_path(win_id, "Container_1\\rich_title");
    if nil ~= rich_text_title and "" ~= rich_text_title then
        ui_utility.setproperty_by_id(static_title_id, 'Visible', 'false');
        ui_utility.setproperty_by_id(rich_title_id, 'Visible', 'true');
        title_id = rich_title_id;
    else
        ui_utility.setproperty_by_id(static_title_id, 'Visible', 'true');
        ui_utility.setproperty_by_id(rich_title_id, 'Visible', 'false');
        title_id = static_title_id;
    end

    -- 只设置内容且没有设置内容标题：统一转换成内容标题进行显示。
    -- 不支持无标题的tips内容
    if text_title == nil or text_title == "" and is_hyperLink == false then
        if text_content ~= nil and text_content ~= "" then
            text_title = text_content;
            text_content = nil;
        end;
    end;

    local has_no_title = false;
    local title_lines = 1;
    if (text_title ~= nil and text_title ~= "") or (rich_text_title ~= nil and rich_text_title ~= "") then
        poptips_view:set_title_text(title_id, text_title, rich_text_title);
        ui_utility.setproperty_by_id(title_id, 'XOffset', content_xoffset);
        ui_utility.setproperty_by_id(title_id, 'YOffset', icon_group_yoffset+15);
        local pShowTitleConten = uilib:GetControlPtr(title_id);
        local textRect = pShowTitleConten:GetRectEx(TRUE, TRUE);
        local textW = textRect.right - textRect.left;
        -- 尚无获取控件字号方法，根据tpf中设置，标题字号为16
        if rich_text_title ~= nil and rich_text_title ~= "" then
            text_title = rich_text_title;
        end
        max_width = poptips_view:resize_max_width(text_title, textW, max_width, opts.auto_line, 16);
        if textW >= max_width then
            ui_utility.setproperty_by_id(title_id, 'MultiLine', "true");
            ui_utility.setproperty_by_id(title_id, 'AutoChangeLine', "true");
            ui_utility.setproperty_by_id(title_id, 'Width', max_width);
            local t1,t2 = math.modf(textW/max_width);
            title_lines = math.ceil(textW/max_width);
            local title_ptr = ui_utility.get_ctrl_ptr_by_id(title_id);
            local title_height = 0;
            if nil ~= title_ptr then
                local rct = title_ptr:GetRectEx(TRUE, TRUE);
                title_height = rct.bottom - rct.top;
            end
            local cur_yoffset = icon_group_yoffset+(20 - title_height / 2);
            if cur_yoffset < 0 then
                cur_yoffset = 0;
            end;
            if opts.max_title_height ~= nil and (t1 * 20) > opts.max_title_height then
                cur_yoffset = 20;
                ui_utility.setproperty_by_id(title_id, 'AutoSize', 'false');
                ui_utility.setproperty_by_id(title_id, 'Height', opts.max_title_height);
            end
            ui_utility.setproperty_by_id(title_id, 'YOffset', cur_yoffset);
        end;
    else
        ui_utility.setproperty_by_id(title_id, 'Visible', 'false');
        has_no_title = true;
    end;

    if has_no_title then
        ui_utility.setproperty_by_id(show_conten_id, 'TextColor', poptips_colors.kTitleNormal);
    end

    if is_hyperLink and nil ~= opts.hyperLinkText and nil ~= text_content then
        local hyperLinkText = '<a>'..opts.hyperLinkText.."</a>";
        local desc = (string.gsub(text_content, "%%LinkText%%", hyperLinkText));
        text_content = desc;
    end

    ui_utility.setproperty_by_id(show_conten_id, 'Caption', text_content);
    if text_content ~= nil and text_content ~= "" then
        local pShowConten = uilib:GetControlPtr(show_conten_id);
        local textRect = pShowConten:GetRectEx(TRUE,TRUE);
        local textW = textRect.right - textRect.left;
        -- 尚无获取控件字号方法，根据tpf中设置，内容字号为14
        max_width = poptips_view:resize_max_width(text_content, textW, max_width, opts.auto_line, 14);
        if textW > max_width then
            ui_utility.setproperty_by_id(show_conten_id, 'AutoChangeLine', "true");
            ui_utility.setproperty_by_id(show_conten_id, 'MultiLine', "true");
            ui_utility.setproperty_by_id(show_conten_id, 'MaxWidth', max_width);
            ui_utility.setproperty_by_id(show_conten_id, 'Width', max_width);
        end;
        ui_utility.setproperty_by_id(show_conten_id, 'XOffset', content_xoffset);
        if text_title == nil or text_title == "" then
            ui_utility.setproperty_by_id(show_conten_id, 'YOffset', icon_group_yoffset+5);
        else
            ui_utility.setproperty_by_id(title_id, 'YOffset', icon_group_yoffset-8);
            ui_utility.setproperty_by_id(show_conten_id, 'YOffset', icon_group_yoffset+20*title_lines);
        end;
    else
        ui_utility.setproperty_by_id(text_id, 'Visible', "false");
        ui_utility.setproperty_by_id(link_id, 'Visible', "false");
    end;
    

    -- 不再询问勾选框
    local cbx_dont_ask_id = ui_utility.get_ctrl_id_by_path(win_id,   "Container_1\\cbx_dont_ask");  
    ui_utility.setproperty_by_id(cbx_dont_ask_id, 'Visible', tostring(opts.cbx_dont_ask.visible)); 
    ui_utility.setproperty_by_id(cbx_dont_ask_id, 'Caption', tostring(opts.cbx_dont_ask.text));
    ui_utility.setproperty_by_id(cbx_dont_ask_id, 'Checked', tostring(opts.cbx_dont_ask.checked));
    ui_utility.setproperty_by_id(cbx_dont_ask_id,  'YOffset',tostring(tonumber(opts.height)-40-34));

    -- 跳转链接
    local link_goto_id = ui_utility.get_ctrl_id_by_path(win_id,   "Container_1\\link_goto");  
    ui_utility.setproperty_by_id(link_goto_id, 'Visible', tostring(opts.link_goto.visible)); 
    ui_utility.setproperty_by_id(link_goto_id, 'Caption', tostring(opts.link_goto.text));
    ui_utility.setproperty_by_id(link_goto_id,  'YOffset', tostring(tonumber(opts.height)-40-34));
    
    if tostring(opts.auto_line_gray) == "true" then
        local line_gray = ui_utility.get_ctrl_id_by_path(win_id, "line_gray");
        ui_utility.setproperty_by_id(line_gray, "Width", tostring(tonumber(opts.width) - self.shadow_px));
        ui_utility.setproperty_by_id(line_gray, "YOffset", tostring(tonumber(opts.height) - 70));
    end;

    -- 按钮位置
    local btn_group_id = ui_utility.get_ctrl_id_by_path(win_id, "button_grup");
    local ok_btn_id = ui_utility.get_ctrl_id_by_path(win_id, "button_grup\\ok");
    local cancel_btn_id = ui_utility.get_ctrl_id_by_path(win_id, "button_grup\\cancel")
    local extra_btn_id = ui_utility.get_ctrl_id_by_path(win_id, "button_grup\\extra")

    ui_utility.setproperty_by_id(ok_btn_id, 'Visible', tostring(opts.okBtn.visible));
    ui_utility.setproperty_by_id(cancel_btn_id, 'Visible', tostring(opts.cancelBtn.visible));
    ui_utility.setproperty_by_id(extra_btn_id, 'Visible', tostring(opts.extraBtn.visible));
    ui_utility.setproperty_by_id(ok_btn_id, 'Caption', opts.okBtn.text);
    ui_utility.setproperty_by_id(cancel_btn_id, 'Caption', opts.cancelBtn.text);
    ui_utility.setproperty_by_id(extra_btn_id, 'Caption', opts.extraBtn.text);

    ui_utility.setproperty_by_id(btn_group_id, 'XOffset', opts.width-16-210-12);
    ui_utility.setproperty_by_id(btn_group_id, 'YOffset', opts.height-26-11-26);

    if tostring(opts.okBtn.visible) == "false" or tostring(opts.cancelBtn.visible) == "false" then
        if tostring(opts.okBtn.visible) == "false" and tostring(opts.cancelBtn.visible) == "false" then
            btnGroupW = 0;
        else
            if tostring(opts.okBtn.visible) == "false" then
                ui_utility.setproperty_by_id(cancel_btn_id, 'XOffset', '0');
            elseif tostring(opts.cancelBtn.visible) == "false" then
                --ui_utility.setproperty_by_id(btn_group_id, 'XOffset', opts.width-self.shadow_px-12-100);
                -- 'change' cancel btn to ok
                ui_utility.setproperty_by_id(ok_btn_id, 'Visible', 'false');
                ui_utility.setproperty_by_id(cancel_btn_id, 'Caption', opts.okBtn.text);
                ui_utility.setproperty_by_id(cancel_btn_id, 'Visible', 'true');
                ui_utility.setproperty_by_id(cancel_btn_id, 'OnClick', 'poptips_view:okBtnClick');
            end;
        end;
    end;
end;

function poptips_view:set_title_text(title_id, text_title, rich_text_title)
    if nil == rich_text_title or "" == rich_text_title then
        ui_utility.setproperty_by_id(title_id, 'Caption', text_title);
    else
        local start_pos = string.find(rich_text_title, "<a>");
        local end_pos = string.find(rich_text_title, "</a>");
        if nil ~= start_pos and nil ~= end_pos and end_pos > start_pos then
            poptips_model.url = string.sub(rich_text_title, start_pos + 3, end_pos - 1);
        end

        local pRichText = ui_utility.get_ctrl_ptr_by_id(title_id);
        if pRichText ~= nil then
            local sTemp = U8toCUIStr(rich_text_title);
            pRichText:Clear();
            pRichText:PushBackHtmlString(sTemp);
        end
    end
end

function poptips_view:resize_max_width(text, text_width, max_width, auto, char_font)
    local auto_line = auto or "auto";
    if auto == "none" then
        -- 关闭自动调整行宽度
        ierd_tgp.tpf_ui.Log("[poptips_view]resize_max_width: close auto_line");
        return max_width;
    end

    local char_width = char_font or 16; -- 默认为16号字体，宽度16
    local min_bound = 1.0 * char_width / max_width;
    local max_bound = 1 - 0.8 * char_width / max_width;
    local t1,t2 = math.modf(text_width/max_width);
    -- 保证最后一行有2个字符，并且最后一行不会很挤
    if t2 >= min_bound and t2 <= max_bound then
        -- 无需调整行宽度
        ierd_tgp.tpf_ui.Log("[poptips_view]resize_max_width: no need to resize width:" 
                            .. "text_width:" .. text_width .. ", max_width:" .. max_width
                            .. ", auto_line:" .. auto .. ", char_font:" .. char_font); 
        return max_width;
    end

    local width;
    -- 先看扩长能否满足要求
    if auto == "auto" or auto == "wide" then
        for width = max_width + char_font, max_width + 1 * char_width, char_font do
            local t1,t2 = math.modf(text_width/width);
            if t2 >= min_bound and t2 <= max_bound then
                ierd_tgp.tpf_ui.Log("[poptips_view]resize_max_width: larger width: resize_width:" .. width  
                                    .. ", text_width:" .. text_width .. ", max_width:" .. max_width
                                    .. ", auto_line:" .. auto .. ", char_font:" .. char_font);
                return width;
            end       
        end 
    end 

    -- 再看缩短能否满足要求
    if auto == "auto" or auto == "narrow" then
        for width = max_width - char_font, max_width - 2 * char_width, -char_font do
            local t1,t2 = math.modf(text_width/width);
            if t2 >= min_bound and t2 <= max_bound then
                ierd_tgp.tpf_ui.Log("[poptips_view]resize_max_width: narrow width: resize_width:" .. width  
                                    .. ", text_width:" .. text_width .. ", max_width:" .. max_width
                                    .. ", auto_line:" .. auto .. ", char_font:" .. char_font);
                return width;
            end       
        end     
    end

    -- 无法满足要求
    ierd_tgp.tpf_ui.Log("[poptips_view]resize_max_width: unable to resize width, text:" .. text 
                            .. ", text_width:" .. text_width .. ", max_width:" .. max_width
                            .. ", auto_line:" .. auto .. ", char_font:" .. char_font, true); 
    return max_width;
end;

function poptips_view:enable_ok_btn(wnd_id, enabled, reset_desc)
    if wnd_id == 0 then
        return;
    end;
    local uilib = ui_utility.uilib();
    local ok_btn_id = ui_utility.get_ctrl_id_by_path(wnd_id, "button_grup\\ok");
    local ico_loading_id = ui_utility.get_ctrl_id_by_path(wnd_id, "Container_loading");
    if (ok_btn_id == 0) or (ico_loading_id == 0)then
        return;
    end;

    ui_utility.setproperty_by_id(ok_btn_id, 'Disable', tostring(not enabled));
    ui_utility.setproperty_by_id(ico_loading_id, 'Visible', tostring(not enabled));
    
    -- 云存档同步完成
    if reset_desc then
        local text_id = ui_utility.get_ctrl_id_by_path(wnd_id, "Container_1\\content");
        if text_id == 0 then
            return;
        end;
        local pConTitleControl = uilib:GetControlPtr(text_id);
        if pConTitleControl == nil then
            return;
        end;
        local static_desc = tolua.cast(pConTitleControl, "Tenio::ITStatic");
        if static_desc == NULL then
            return;
        end;
        static_desc:SetText(poptips_view:toStr(LoadStr("POP_TIPS_CLOUD_DESC")));
    end;
end;

-- 配置poptips
function poptips_view:set_config(config)
    -- 默认布局自定义参数设置
    local defaultCustomParams = {
        -- 以下下为默认布局设置参数
        -- 以下参数如果是自订一布局可以不用设置
        -- 参数值: ONEIMAGE:login\\base\\2\\login_bg.jpg
        title = LoadStr("WEGAME_TIPS_DEF_TITLE"),
        titleIcon = "ONEIMAGE:login\\icon\\logo.png;Stretch;null;",
        -- "ONEIMAGE:main\\default_icon.png;Center;null;",
        icon = nil,
        iWidth = nil,
        iHeight = nil,
        textSize = 12,
        textWeight = 400,
        tipsIcon = poptips_icons.kError,
        descTitle = nil,
        desc = LoadStr("POP_TIPS_DEFAULT_DESC"),
        user_data = nil,
        -- 是否文本为超链接
        hyperLink = false,
        -- 超链接操作回调函数
        auto_line = "auto",
        -- 是否自动调整一行长度   --"auto":自动调整, "none":不调整, "large":往大调整, "narrow":往小调整
        hyperLinkHandle = function(uilib, win_id)
            poptips_view:new_win_close(uilib, win_id);
        end,      
        cbx_dont_ask = {
            visible = false;
            text = ""; 
            checked = false;
        },
        link_goto = {
            visible = false;
            text = LoadStr("POP_TIPS_DEFAULT_LINKTEXT");
            link = "{\"type\":\"my_assets\"}";
        },
        extraBtn = {
            visible = false;
            text = "",
            handle = function(uilib, ctr_id)
            end
        },
        okBtn = {
            visible = true;
            text = LoadStr("POP_TIPS_DEFAULT_OK"),
            close_win = true;
            handle = function(uilib, win_id)
                poptips_view:new_win_close(uilib, win_id);
            end
        },
        cancelBtn = {
            visible = true,
            text = LoadStr("POP_TIPS_DEFAULT_CANCEL"),
            handle = function()
            end
        },
        close_means_cancel = true  -- 关闭时等同于取消

        -- 如果是自定义布局，请指定UI布局渲染函数
        -- 自定义布局时，关闭按钮请放在空间树第三级，如：
        -- layout_callback = "local_web_wnd.layout",
        -- 自定义布局需引入的文件，如：
        -- layout_file = "local_web\\local_web_wnd.lua"
    }

    --Default setting
    local defaultParam = {
        -- 参数值: rbottom-右下角，center-居中
        position = 'center',
        width = self.width,
        height = self.height,
        -- 是否需要模态置顶 (多窗口模式下不能设置模态置顶)
        modal = true,
        -- 手动关闭时的操作回调
        manuallyCloseCallback = function()
            return;
        end,
        -- 自动关闭时的操作回调
        autoCloseCallback = function()
            return;
        end,
        -- 是否为多窗口模式
        is_multi_win = true,
    };

    -- 遍历并配置poptips
    for key, value in pairs(defaultCustomParams) do
        defaultParam[key] = value;
    end;

    -- 遍历并配置poptips
    for key, value in pairs(config) do
        defaultParam[key] = value;
    end;

    self._settings = defaultParam;
    self._settings.width = self._settings.width + self.shadow_px; -- 整体宽度加上阴影部分
    self._settings.height = self._settings.height + self.shadow_px;
    self.ui_highlight_color = poptips_colors.kHighlight;
    if base_utility.is_type_of_oversea_ver() then
        self.ui_highlight_color = poptips_colors.kHighlightOversea;
    end;
end;

function poptips_view:load_layout()
    -- 创建布局
    local opts = self._settings;
    local win_id = 0;
    --
    if opts.ui_file then
        if opts.is_multi_win then
            win_id = layout_mgr.restore_layout_multi(opts.ui_file);
        else
            win_id = layout_mgr.restore_layout(opts.ui_file);
        end;
    else
        local ui_file = "poptips.tpf";
        if opts.is_multi_win then
            win_id = layout_mgr.restore_layout_multi(ui_file);
        else
            local parent_wnd = nil;
            if opts.parent_id then   
                ierd_tgp.tpf_ui.Log("[poptips_view]getting parent_wnd by parent_id:" ..
                             tostring(opts.parent_id)); 
                parent_wnd = ui_utility.get_ctrl_ptr_by_id(opts.parent_id);
            end;
            if parent_wnd then   
                ierd_tgp.tpf_ui.Log("[poptips_view]restore layout under parent which is existed");
                win_id = layout_mgr.restore_layout_ex(opts.parent_id, ui_file);
            else
                win_id = layout_mgr.restore_layout(ui_file);
            end
        end
    end
    base_utility.log("[poptips_view][load_layout]got win_id:" .. tostring(win_id));
    self.win_id = win_id;

    poptips_model.ctr_data[win_id] = opts;
end;

-- 使用字符串调用函数
-- TODO 增加使用“:”调用的支持
function poptips_view:findfunction(x)
    assert(type(x) == "string")
    local f=_G
    for v in x:gmatch("[^%.]+") do
        if type(f) ~= "table" then
            return nil, "looking for '"..v.."' expected table, not "..type(f)
        end
        f=f[v]
    end
    if type(f) == "function" then
        return f
    else
        return nil, "expected function, not "..type(f)
    end
end

-- 窗口初始化
function poptips_view:init_window(win_id)
    if win_id == 0 then return end;

    local opts = self._settings;
    local uilib = ui_utility.uilib();
    local pControl = uilib:GetControlPtr(win_id);
    local pPoptipsDlg = tolua.cast(pControl, "Tenio::ITDialog");

    local parent = nil;
    --窗体位置
    if opts.parent_id then
        -- 显示在父窗口正中间
        parent = ui_utility.get_ctrl_ptr_by_id(opts.parent_id);
    end

    if parent then
        local parent_rect =  parent:GetRectEx(FALSE,TRUE); 
        local parent_width = parent_rect.right - parent_rect.left;
        local parent_hegiht = parent_rect.bottom - parent_rect.top;
        local rc_self = pPoptipsDlg:GetRectEx(TRUE,TRUE);

        if opts.parent_id == ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame) and 
            opts.always_center == true then
            ui_utility.setproperty_by_id(win_id, 'XOffset', tostring((opts.swidth 
                - opts.width)/2 - parent_rect.left));
            ui_utility.setproperty_by_id(win_id, 'YOffset', tostring((opts.sheight 
                - opts.height)/2 - parent_rect.top));
        else
            rc_self.left = parent_rect.left + (parent_width - opts.width) / 2;
            rc_self.right = rc_self.left + opts.width;
            rc_self.top = parent_rect.top + (parent_hegiht - opts.height) / 2;
            rc_self.bottom = rc_self.top + opts.height;
            pPoptipsDlg:MoveEx(rc_self, TRUE);
        end;
    elseif opts.position == 'center' then
        ui_utility.setproperty_by_id(win_id, 'XOffset', tostring((tonumber(opts.swidth) 
        - tonumber(opts.width))*0.5 + tonumber(opts.sx)));
        ui_utility.setproperty_by_id(win_id, 'YOffset', tostring((tonumber(opts.sheight) 
        - tonumber(opts.height))*0.5 + tonumber(opts.sy)));
    elseif opts.position == 'rbottom' then
        ui_utility.setproperty_by_id(win_id, 'XOffset', tostring(tonumber(opts.swidth) 
        - tonumber(opts.width) + tonumber(opts.sx)));
        ui_utility.setproperty_by_id(win_id, 'YOffset', tostring(tonumber(opts.sheight) 
        - tonumber(opts.height) + tonumber(opts.sy)));
    end;

    --窗体尺寸
    ui_utility.setproperty_by_id(win_id, 'Width', tostring(opts.width));
    ui_utility.setproperty_by_id(win_id, 'Height', tostring(opts.height));
    
    -- 开始布局
    if opts.layout_file then
        ierd_tgp.tpf_ui.Require(opts.layout_file);
        self:findfunction(opts.layout_callback)(win_id, opts);
    else
        self:default_layout(win_id);
    end

    local show_now = false;
    if opts.delay_show_wnd == nil or opts.delay_show_wnd == false then
        show_now = true;
    end;

    if show_now then
        ierd_tgp.tpf_ui.Log("[poptips_view]show now " .. tostring(win_id));
        pPoptipsDlg:Show(TRUE);
        pPoptipsDlg:BringToTop();

        if tostring(opts.modal) == "true" then
            ui_utility.do_fake_modal_by_id(win_id);
        end;
    else
        ierd_tgp.tpf_ui.Log("[poptips_view]not show now " .. tostring(win_id));
        pPoptipsDlg:Show(FALSE);
    end;

end;

-- 定时关闭窗口
function poptips_view:timing_close(win_id, time_stamp)
    local uilib = ui_utility.uilib();
    local opts = self:get_ctr_data(win_id);
    
    if self:_close(uilib, win_id) then
        if opts.autoCloseCallback then
            opts.autoCloseCallback(win_id);
            if (server_tips_view ~= nil) then
                server_tips_view.cancelQblinkRestartListener();
            end
        end
    end

    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval("poptips_view:timing_close",
    win_id)
end;

-- 设置自动关闭窗口
function poptips_view:set_auto_close()
    local opts = self._settings;
    if opts.timer then
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval("poptips_view:timing_close",
        opts.timer, self.win_id);
    end;
end;

function poptips_view:show(param)
    ierd_tgp.tpf_ui.Log("[poptips_view]show, parent_id:" .. tostring(param.parent_id));
    -- 加载配置
    self:set_config(param);
    self.last_opt_ = param;
    self:load_layout();
    self:init_window(self.win_id);
    self:set_auto_close();
    return self.win_id;
end;


function poptips_view:open_wifi_link(uilib, ctr_id)
    base_utility.log("[wifi_tips_view] ready to open wifi guide.");

    -- 打开wifi网页
    -- 由于要控制窗口大小，所以采用以下接口
    local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
    local common_info = ierd_tgp.game_library.Common_info_export:new_local();
    if game_lib == nil or common_info == nil then
        return;
    end;
    game_lib:get_common_info(common_info);
    
    local wifi_guide_url = common_info.tgp_web_url .. 'mtips/wifi_guide.html';

    web_utility.open_new_browser(wifi_guide_url, false, 
        false, false, LoadStr("WIFI_HELPER_ROUTER"), 1000, 700, 0);

    -- 关闭弹窗
    local content_id = ui_utility.get_ctrl_parent_id(ctr_id);
    if content_id <= 0 then
        return;
    end
    local win_id = ui_utility.get_ctrl_parent_id(content_id);
    if win_id <= 0 then
        return;
    end

    poptips_view:_close(uilib, win_id);
end

function poptips_view:OnTitleLinkClick(uilib, ctr_id)
    if "" == poptips_model.url then
        return;
    end

    web_utility.open_browser(poptips_model.url, false, true);
end

-- 进行设置，并获取初始数据
function poptips_view:on_srv_poptips_setup(data_in)
    base_utility.log("[poptips_view]on_srv_poptips_setup " ..
            base_utility.table_to_json(data_in));

    if not data_in.win_id then
        base_utility.log("[poptips_view][on_srv_poptips_setup]win_id REQUIRED, " ..
                base_utility.table_to_json(data_in));
        return;
    end

    local win_id = tonumber(data_in.win_id);

    if data_in.width ~= nil and data_in.height ~= nil then
        local width = tonumber(data_in.width);
        local height = tonumber(data_in.height);
        ui_utility.control_resize_by_id(win_id, width, height);
    end

    local src_opt = poptips_model.ctr_data[win_id];

    if not src_opt then
        base_utility.log("[poptips_view][on_srv_poptips_setup]opt NOT EXIST, " ..
                base_utility.table_to_json(data_in));
        return;
    end

    local opt = base_utility.copy_table_without_function(src_opt);

    if opt.cancelBtn then
        opt.cancelBtn.js_call_back = "jc_cancel_clicked";
    end

    if opt.okBtn then
        opt.okBtn.js_call_back = "jc_ok_clicked";
    end

    if opt.extraBtn then
        opt.extraBtn.js_call_back = "jc_extra_clicked";
    end

    ui_utility.set_wnd_opts(win_id, opt);

    return opt;
end;


poptips_view.init();
